package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;

@SuppressLint("SimpleDateFormat")
public class UpdateAddressWSControl extends BaseWSControl {
	private String userId;
	private String address;
	private String zipCode;
	private String name;
	private String note;

	public UpdateAddressWSControl(Context context,
			WebServiceCommunicatorListener listener, String userId,
			String address, String zipCode, String name, String note) {
		super(context, listener, WebServiceFlag.ADD_ADDRESS);
		this.address = address;
		this.zipCode = zipCode;
		this.name = name;
		this.note = note;
	}

	@Override
	protected Object doInBackground(Void... ps) {
		// Get local database

		String url = Statics.ADMIN_WS_URL + "account/address/" + userId;

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("zip", zipCode);
		params.put("name", name);
		params.put("note", note);
		params.put("address", address);

		try {
			String respone = NetworkUtils.doPostRequest(url, params);
			try {
				JSONObject jsonObject = new JSONObject(respone);

				return jsonObject.getString("error");
			} catch (JSONException e) {
			}

			return respone;
		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return mError;
	}

}
